package com.wudl.schedule.service.impl;

import com.wudl.schedule.service.ScheduleService;
import com.wudl.schedule.utils.RestTemplateUtils;
import com.wudl.schedule.utils.Result;
import com.wudl.schedule.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * @author ：wudl
 * @date ：Created in 2022-03-14 17:28
 * @description：
 * @modified By：
 * @version: 1.0
 */

@Service
@Slf4j
public class ScheduleServiceImpl implements ScheduleService {


    @Override
    public Result getTaskStateCount(String startDate ,String endDate) {


        String urltoken = "http://192.168.1.162:12345/dolphinscheduler/projects/analysis/task-state-count?projectId=0&startDate=%s&endDate=%s&_t=0.10698359456177409";

        String newUrl = String.format(urltoken,startDate,endDate);
        log.info("获取任务的统计----------url{}",newUrl);
        HttpHeaders requestHeaders = TokenUtils.getHttpHeaders();
        ResponseEntity<Result> resultResponseEntity = RestTemplateUtils.get(newUrl, requestHeaders, Result.class);
        return resultResponseEntity.getBody();
    }

    @Override
    public Result getProcessStateCount(String startDate ,String endDate) {

        String urltoken = "http://192.168.1.162:12345/dolphinscheduler/projects/analysis/process-state-count?projectId=0&startDate=%s&endDate=%s&_t=0.49008884316645696";
        String newUrl = String.format(urltoken,startDate,endDate);
        log.info("获取任务的统计----------url{}",newUrl);
        HttpHeaders requestHeaders = TokenUtils.getHttpHeaders();
        ResponseEntity<Result> resultResponseEntity = RestTemplateUtils.get(newUrl, requestHeaders, Result.class);
        return resultResponseEntity.getBody();
    }

    @Override
    public Result getDefineUserCount(Integer projectId) {
        String urltoken = "http://192.168.1.162:12345/dolphinscheduler/projects/analysis/define-user-count?projectId=%s&_t=0.9267691294419027";
        String newUrl = String.format(urltoken,projectId);
        log.info("获取任务的统计----------url{}",newUrl);
        HttpHeaders requestHeaders = TokenUtils.getHttpHeaders();
        ResponseEntity<Result> resultResponseEntity = RestTemplateUtils.get(newUrl, requestHeaders, Result.class);
        return resultResponseEntity.getBody();
    }

    @Override
    public Result getCommandStateCount() {
        String urltoken = "http://192.168.1.162:12345/dolphinscheduler/projects/analysis/command-state-count?_t=0.7372064459407579";
        log.info("获取任务的统计----------url{}",urltoken);
        HttpHeaders requestHeaders = TokenUtils.getHttpHeaders();
        ResponseEntity<Result> resultResponseEntity = RestTemplateUtils.get(urltoken, requestHeaders, Result.class);
        return resultResponseEntity.getBody();
    }

    @Override
    public Result getQueueCount() {
        String url = "http://192.168.1.162:12345/dolphinscheduler/projects/analysis/queue-count?_t=0.9112382637265675";
        log.info("获取任务的统计----------url{}",url);
        HttpHeaders requestHeaders = TokenUtils.getHttpHeaders();
        ResponseEntity<Result> resultResponseEntity = RestTemplateUtils.get(url, requestHeaders, Result.class);
        return resultResponseEntity.getBody();
    }
}
